<template>
  <div class="container mt-5">
    <div class="row">
      <div class="col-12">
        <div class="card">
          <div class="card-header">
            <h3 class="mb-0"><i class="fas fa-landmark mr-2"></i>添加文化遗产</h3>
          </div>
          <div class="card-body">
            <form @submit.prevent="submitForm">
              <div class="form-group">
                <label for="name">名称</label>
                <input type="text" class="form-control" id="name" v-model="form.name" required>
              </div>
              
              <div class="form-group">
                <label for="type">类型</label>
                <select class="form-control" id="type" v-model="form.type" required>
                  <option value="">请选择类型</option>
                  <option v-for="type in types" :key="type" :value="type">{{ type }}</option>
                </select>
              </div>
              
              <div class="form-group">
                <label for="location">地点</label>
                <input type="text" class="form-control" id="location" v-model="form.location" required>
              </div>
              
              <div class="form-group">
                <label for="year">年代</label>
                <input type="text" class="form-control" id="year" v-model="form.year">
              </div>
              
              <div class="form-group">
                <label for="protectionLevel">保护级别</label>
                <select class="form-control" id="protectionLevel" v-model="form.protectionLevel" required>
                  <option value="">请选择保护级别</option>
                  <option v-for="level in protectionLevels" :key="level" :value="level">{{ level }}</option>
                </select>
              </div>
              
              <div class="form-group">
                <label for="description">描述</label>
                <textarea class="form-control" id="description" v-model="form.description" rows="4"></textarea>
              </div>
              
              <div class="form-group">
                <label for="image">图片</label>
                <input type="file" class="form-control-file" id="image" @change="handleImageUpload">
                <small class="form-text text-muted">支持 JPG、PNG 格式，大小不超过 2MB</small>
                <img v-if="form.imageUrl" :src="form.imageUrl" class="img-thumbnail mt-2" style="max-width: 200px; max-height: 200px;">
              </div>
              
              <div class="mt-4">
                <button type="submit" class="btn btn-primary mr-2">
                  <i class="fas fa-save mr-1"></i>保存
                </button>
                <button type="button" class="btn btn-secondary" @click="$router.go(-1)">
                  <i class="fas fa-arrow-left mr-1"></i>返回
                </button>
              </div>
            </form>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { createHeritage } from '@/api/api';

export default {
  name: 'HeritageAdd',
  data() {
    return {
      form: {
        name: '',
        type: '',
        location: '',
        year: '',
        protectionLevel: '',
        description: '',
        imageUrl: ''
      },
      types: ['建筑', '文物', '遗址', '民俗'],
      protectionLevels: ['国家级', '省级', '市级', '县级']
    }
  },
  methods: {
    handleImageUpload(event) {
      const file = event.target.files[0];
      if (file) {
        // 这里可以添加上传图片到服务器的逻辑
        // 上传成功后，将返回的图片URL赋值给form.imageUrl
        // 示例：this.form.imageUrl = 'https://example.com/upload/' + file.name;
        console.log('上传文件:', file);
        this.form.imageUrl = 'https://example.com/upload/' + file.name; // 临时模拟
      }
    },
    async submitForm() {
      try {
        // 使用api.js中的createHeritage方法
        await createHeritage(this.form);
        this.$message.success('保存成功');
        this.$router.push('/heritage');
      } catch (error) {
        console.error('保存失败:', error);
        this.$message.error('保存失败: ' + (error.response?.data?.message || error.message));
      }
    }
  }
}
</script>

<style scoped>
/* 卡片样式 */
.card {
  border: none;
  border-radius: 15px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  background: linear-gradient(145deg, #ffffff 0%, #f8f9fa 100%);
}

/* 卡片头部样式 */
.card-header {
  background-color: #2c3e50;
  color: white;
  border-bottom: none;
  padding: 1.5rem 1.5rem 1rem;
  border-radius: 15px 15px 0 0;
}

.card-header h3 {
  margin: 0;
  font-size: 1.5rem;
  font-weight: 600;
  display: flex;
  align-items: center;
}

.card-header i {
  color: #3498db;
  margin-right: 0.5rem;
  font-size: 1.2rem;
}

/* 表单样式 */
.form-group {
  margin-bottom: 1.75rem;
}

.form-group label {
  font-weight: 500;
  margin-bottom: 0.75rem;
  font-size: 0.95rem;
  color: #333;
}

.form-control {
  border: 1px solid #e9ecef;
  border-radius: 8px;
  padding: 0.85rem 1rem;
  font-size: 0.95rem;
  transition: all 0.3s ease;
}

.form-control:focus {
  border-color: #3498db;
  box-shadow: 0 0 0 0.2rem rgba(52, 152, 219, 0.25);
}

/* 选择框样式 */
select.form-control {
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  background-image: url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%236c757d' d='M6 8L2 4l1.5-1.5L6 5.25 9.5 1.75 11 3l-5 5z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 0.75rem center;
  background-size: 12px 12px;
  padding-right: 2.5rem;
}

/* 文本区域样式 */
textarea.form-control {
  min-height: 150px;
  resize: vertical;
  padding: 1rem;
}

/* 上传图片样式 */
.form-control-file {
  padding: 0.75rem;
  border: 1px dashed #e9ecef;
  border-radius: 8px;
  background: #f8f9fa;
  color: #6c757d;
  cursor: pointer;
  transition: all 0.3s ease;
}

.form-control-file:hover {
  background: #f1f3f5;
  border-color: #3498db;
}

/* 按钮样式 */
.btn {
  padding: 0.85rem 1.75rem;
  border-radius: 8px;
  font-weight: 500;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 0.6rem;
}

.btn-primary {
  background-color: #3498db;
  border: none;
}

.btn-primary:hover {
  background-color: #2980b9;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(52, 152, 219, 0.2);
}

.btn-secondary {
  background-color: #6c757d;
  border: none;
}

.btn-secondary:hover {
  background-color: #5a6268;
  transform: translateY(-1px);
}

/* 间距类 */
.mr-2 {
  margin-right: 0.75rem;
}

.mr-1 {
  margin-right: 0.5rem;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .card-header h3 {
    font-size: 1.25rem;
  }
  
  .form-control {
    padding: 0.7rem 0.85rem;
    font-size: 0.9rem;
  }
  
  .btn {
    padding: 0.7rem 1.4rem;
    font-size: 0.9rem;
  }
  
  .form-group {
    margin-bottom: 1.5rem;
  }
}
</style>
